/**
 * @FileName: CmccService.java
 * @Author
 * @Description:中转服务业务类
 * @Date 2016年5月16日 下午7:20:50
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.google.common.net.MediaType;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.util.DESAS;
import cn.hpclub.server.util.HttpUtil;
import cn.hpclub.server.util.SystemConfigUtil;

public class CmccService{
    private static final Logger logger           = LoggerFactory.getLogger(CmccService.class);
    public static CmccService   service          = new CmccService();
    private String              CMCC_HOST_PREFIX = SystemConfigUtil.getSystemConfig().getCmccHostPrefix();
    private String              REST_HOST_PREFIX = SystemConfigUtil.getSystemConfig().getRestHostPrefix();

    // /**
    // * @Title: getActivities
    // * @Description:TODO
    // * @param mobile
    // * @param modelCode
    // * @param token
    // * @return String
    // * @throws GetActivitiesExcettion
    // */
    // public String getActivities(String mobile, String modelCode, String
    // token) throws GetActivitiesExcettion{
    // String result = "";
    // try{
    // Record memberInfo = TokenService.service.getMemberOrgInfoByToken(token);
    // if(memberInfo != null){
    // HashMap<String, Object> hm = new HashMap<String, Object>();
    // // hm.put("resource_code", modelCode);// 机型编码，必填
    // // hm.put("region_id", 118);// 地市编码，为三位数的地市编码，如广安为118，必填
    // // hm.put("login_no", "hbj801");// 工号，必填
    // // hm.put("group_id", "1045365");// 工号对应的渠道编码，必填
    // // hm.put("phone_no", mobile);// 办理号码
    // hm.put("resource_code", modelCode);// 机型编码，必填
    // hm.put("region_id", memberInfo.getInt(CmccCity.REGION_ID_3));//
    // 地市编码，为三位数的地市编码，如广安为118，必填
    // hm.put("login_no", memberInfo.getStr(MemberOrg.EMP_CODE));// 工号，必填
    // hm.put("group_id", memberInfo.getStr(Organization.NUMCODE));//
    // 工号对应的渠道编码，必填
    // hm.put("phone_no", mobile);// 办理号码
    //
    // result = HttpUtil.getHttpByPost(CMCC_HOST_PREFIX +
    // Constant.Url.CMCC_GET_ACTIVITIES, hm);
    // } else{
    // logger.error("get member_org info error");
    // }
    // }
    // catch(Exception e){
    // throw new GetActivitiesExcettion("cmcc server getActivities
    // Exception!!!*****" + e.toString());
    // // e.printStackTrace();
    // }
    //
    // return result;
    // }

    /**
     * @Title: getActivities
     * @Description:获取终端销售活动列表 RESOURCE_CODE：机型编码、REGION_ID：三位数的地市编码，如广安为118、
     *                         PHONE_NO:办理手机号码、GROUP_ID:工号对应的渠道编码、LOGIN_NO：工号
     * @param searchInfo
     * @return String
     */
    public String getActivities(ConcurrentHashMap<String, String> searchInfo) throws GetActivitiesExcettion{
        String result = "";
        try{
            String body = buildRequest(searchInfo);
            logger.debug("body = " + body);
            logger.debug("url = " + REST_HOST_PREFIX + Constant.Url.CMCC_GET_ACTIVITIES + "?token="
                    + searchInfo.get("TOKEN"));
            result = HttpUtil.syncPostJsonBody(REST_HOST_PREFIX + Constant.Url.CMCC_GET_ACTIVITIES + "?token="
                    + searchInfo.get("TOKEN"), MediaType.JSON_UTF_8, body, 30000);

        }
        catch(Exception e){
            throw new GetActivitiesExcettion("cmcc server getActivities Exception!!!*****" + e.getMessage());
            // e.printStackTrace();
        }

        return result;
    }

    private String buildRequest(ConcurrentHashMap<String, String> searchInfo){
        String phoneNo = searchInfo.get(ClientApiConstant.CmccKey.PHONE_NO_LOWER);
        JSONObject root = new JSONObject();
        JSONObject body = new JSONObject();
        JSONObject requestInfo = new JSONObject();
        requestInfo.put("RESOURCE_CODE", searchInfo.get(ClientApiConstant.CmccKey.RESOURCE_CODE));
        requestInfo.put("RESOURCE_NAME", "");
        requestInfo.put("REGION_ID", searchInfo.get(ClientApiConstant.CmccKey.REGION_ID));
        requestInfo.put("PHONE_NO", phoneNo);
        requestInfo.put("PROVINCE_GROUP", "10008");
        requestInfo.put("GROUP_ID", searchInfo.get(ClientApiConstant.CmccKey.GROUP_ID));
        requestInfo.put("LOGIN_NO", searchInfo.get(ClientApiConstant.CmccKey.LOGIN_NO));
        requestInfo.put("MEANS_ID", "");
        requestInfo.put("MEANS_NAME", "");
        requestInfo.put("DISTRICT_CODE", "");

        // body.put("PHONE_NO", phoneNo);
        body.put("REQUEST_INFO", requestInfo);

        root.put("BODY", body);
        return root.toString();
    }

    /**
     * @Title: getValidVerifyCodeResult
     * @Description:TODO
     * @param mobile
     * @param verifyCode
     * @return String
     * @throws ValidVerifyCodeException
     */
    public String getValidVerifyCodeResult(String mobile, String verifyCode) throws ValidVerifyCodeException{
        String result = "";
        try{
            // 本地编码
            String encodeParams = getValidVerifyCodeParams(mobile, verifyCode);
            logger.info("encodeParams = " + encodeParams);
            HashMap<String, Object> hm = new HashMap<String, Object>();
            hm.put(H5ApiConstant.CmccKey.RAWSTR, encodeParams);

            result = HttpUtil.getHttpByPost(CMCC_HOST_PREFIX + Constant.Url.VALID_RAND_PASS, hm);
        }
        catch(Exception e){
            throw new ValidVerifyCodeException("cmcc server validRandPass Exception!!!*****" + e.toString());
        }

        return result;
    }



    public String getValidVerifyCodeResultForRest(String mobile, String verifyCode,String loginno) throws ValidVerifyCodeException{
        String result = "";
        try{
            // 本地编码
            String encodeParams = getValidVerifyCodeParams(mobile, verifyCode);
            logger.info("encodeParams = " + encodeParams);
            HashMap<String, Object> hm = new HashMap<>();
            hm.put("phoneNumber", mobile);
            hm.put("smsCode", verifyCode);// REST_HOST_PREFIX
            hm.put("smsType", 9);
            hm.put("empCode",loginno);
            result = HttpUtil.syncGetParameterMap(REST_HOST_PREFIX + Constant.RestUrl.VALIDATE_SM_CODE, MediaType.JSON_UTF_8, hm);
        }
        catch(Exception e){
            throw new ValidVerifyCodeException("cmcc server validRandPass Exception!!!*****" + e.toString());
            // e.printStackTrace();
        }

        return result;
    }

    /**
     *
     *
     * @Title: getValidVerifyCodeParams
     * @Description:参数编码
     * @param mobile
     * @param verifyCode
     * @return String
     * @throws Exception
     */
    private String getValidVerifyCodeParams(String mobile, String verifyCode) throws Exception{
        String value = "phone_no=" + mobile + "|smsPwd=" + verifyCode;

        return new DESAS().encrypt(value);
    }

    public static class GetActivitiesExcettion extends Exception{
        private static final long serialVersionUID = -616790774645756915L;

        public GetActivitiesExcettion(String message){
            super(message);
        }
    }

    public static class ValidVerifyCodeException extends Exception{
        private static final long serialVersionUID = -5160176058353639373L;

        public ValidVerifyCodeException(String message){
            super(message);
        }
    }

}
